let previewCaptureImg, fileName;

chrome.storage.local.get(["captureImg", "name"], function (result) { 
  previewCaptureImg = result['captureImg'];
  fileName = result['name'];
  document.getElementById("previewCaptureImg").src = previewCaptureImg;
})

document.getElementById("downloadFile").addEventListener("click", function () { 
  if (previewCaptureImg) { 
    downloadFile(fileName, previewCaptureImg);
  }
})

function downloadFile(fileName, imgData) { 
  // 创建a标签模拟点击
  const a = document.createElement("a");
  // href属性设置为图片的base64数据
  a.href = imgData;
  a.download = `${fileName}.png`;
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}